package com.yztc.orangeentertainment.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.orangeentertainment.R;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/10.
 */
public class ChannalArticleContentAdapter extends RecyclerView.Adapter<ChannalArticleContentAdapter
        .MyViewHolderAll> {
    private Context context;
    private List<String> allList;

    public ChannalArticleContentAdapter(Context context, List<String> allList) {
        this.context = context;
        this.allList = allList;
    }

    @Override
    public int getItemViewType(int position) {
        if (allList.get(position).contains("http")) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public MyViewHolderAll onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View view = LayoutInflater.from(context).inflate(R.layout
                    .channalarticlecontent_itemimage,parent, false);

            return new MyViewHolderImage(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout
                    .channalarticlecontent_itemtext, parent,false);
            return new MyViewHolderText(view);
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolderAll holder, int position) {
        if (holder instanceof MyViewHolderImage) {
           if(allList.get(position).contains(".gif")) {
               Glide.with(context).load(allList.get(position)).asGif().into(((MyViewHolderImage) holder)
                       .ivArticlecontentiv);
           }else{
               Glide.with(context).load(allList.get(position)).into(((MyViewHolderImage) holder)
                       .ivArticlecontentiv);

           }
        }
        if(holder instanceof MyViewHolderText){
            if(allList.get(position)!=null) {
                if (position+1 == allList.size()) {
                    TextPaint tp = ((MyViewHolderText) holder).tvArticlecontent.getPaint();
                    tp.setFakeBoldText(true);
                    ((MyViewHolderText) holder).tvArticlecontent.setTextSize(15.0f);
                    ((MyViewHolderText) holder).tvArticlecontent.setText(allList.get(position));

                }else{
                    ((MyViewHolderText) holder).tvArticlecontent.setText(allList.get(position));
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return allList.size();
    }

    static class MyViewHolderAll extends RecyclerView.ViewHolder {

        public MyViewHolderAll(View itemView) {
            super(itemView);
        }
    }

    static class MyViewHolderImage extends MyViewHolderAll {
        @InjectView(R.id.iv_articlecontentiv)
        ImageView ivArticlecontentiv;

        public MyViewHolderImage(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

    class MyViewHolderText extends MyViewHolderAll {
        @InjectView(R.id.tv_articlecontent)
        TextView tvArticlecontent;
        public MyViewHolderText(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }
}
